package com.example.demo.service.order.impl;

import com.alibaba.fastjson.JSON;
import com.example.demo.common.BaseErrorConfigEnum;
import com.example.demo.common.BasePageResponse;
import com.example.demo.common.ResultObject;
import com.example.demo.common.TerminologyConfigEnum;
import com.example.demo.dao.IVirtualDao;
import com.example.demo.dao.IVpsDao;
import com.example.demo.jooq.tables.daos.VirtualDao;
import com.example.demo.jooq.tables.pojos.Virtual;
import com.example.demo.model.po.VirtualPage;
import com.example.demo.model.po.VpsPage;
import com.example.demo.model.request.VirtualPageRequest;
import com.example.demo.model.request.VirtualRequest;
import com.example.demo.model.request.VpsPageRequest;
import com.example.demo.model.response.VirtualResponse;
import com.example.demo.model.response.VpsResponse;
import com.example.demo.service.BaseBusinessServiceImpl;
import com.example.demo.service.order.IVirtualService;
import com.example.demo.service.order.IVpsService;
import com.example.demo.util.AssertUtils;
import com.example.demo.util.ClazzUtils;
import com.example.demo.util.DateUtils;
import org.modelmapper.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class IVirtualServiceImpl extends BaseBusinessServiceImpl implements IVirtualService {
    @Autowired
    private IVirtualDao iVirtualDao;

    @Autowired
    private VirtualDao virtualDao;
    @Override
    public ResultObject<BasePageResponse<VirtualResponse>> queryPageVirtualList(VirtualPageRequest virtualPageRequest) {
        ResultObject<BasePageResponse<VirtualResponse>> responseResultObject = new ResultObject<>();
        List<VirtualPage> vpsPages =  iVirtualDao.queryPageVirtualList(virtualPageRequest);
        if(AssertUtils.isNotEmpty(vpsPages)){
            Integer integer =vpsPages.get(0).getTotalLine();
            List<VirtualResponse> vpsResponses = (List<VirtualResponse>)this.converterList(vpsPages,new TypeToken<List<VirtualResponse>>() {}.getType() );
            //术语转换
            vpsResponses.forEach(visaResponse -> {
                if(AssertUtils.isNotNull(visaResponse.getVirtualStatus())){
                    Arrays.stream(TerminologyConfigEnum.VIRTUAL_STATUS.values()).filter(value->value.name().equals(visaResponse.getVirtualStatus())).forEach(value->visaResponse.setVirtualStatus(value.desc()));
                }
                if(AssertUtils.isNotNull(visaResponse.getSite())){
                    Arrays.stream(TerminologyConfigEnum.SITE.values()).filter(value->value.name().equals(visaResponse.getSite())).forEach(value->visaResponse.setSite(value.desc()));
                }
            });
            BasePageResponse<VirtualResponse> responseBasePageResponse =BasePageResponse.getData(virtualPageRequest.getCurrentPage(),virtualPageRequest.getPageSize(),integer,vpsResponses);
            responseResultObject.setData(responseBasePageResponse);
        }
        return responseResultObject;
    }


    @Override
    public ResultObject<VirtualResponse> queryVirtuaDetail(String virtualId) {
        ResultObject<VirtualResponse> resultObject=new ResultObject<>();
        Virtual virtual = virtualDao.fetchOneByVirtualId(virtualId);
        if(AssertUtils.isNotNull(virtual)){
            VirtualResponse virtualResponse = (VirtualResponse) this.converterObject(virtual,VirtualResponse.class);
            resultObject.setData(virtualResponse);
        }
        return resultObject;
    }

    @Override
    public ResultObject saveVirtual(VirtualRequest virtualRequest) {
        this.getLogger().info("保存virtual,请求数据为:{}", JSON.toJSONString(virtualRequest));
        ResultObject resultObject = new ResultObject();
        //参数校验
        vaildVirtualParameter(virtualRequest);
        Virtual virtual =null;
        if(AssertUtils.isNotNull(virtualRequest.getVirtualId())){
            virtual = virtualDao.fetchOneByVirtualId(virtualRequest.getVirtualId());
            AssertUtils.isNotNull(this.getLogger(),virtual, BaseErrorConfigEnum.VIRTUAL_NOT_NULL);
        }else {
            virtual = new Virtual();
        }
        ClazzUtils.copyPropertiesIgnoreNull(virtualRequest,virtual);
        //持久化
        if(AssertUtils.isNotEmpty(virtualRequest.getVirtualId())){
            virtual.setUpdatedDate(System.currentTimeMillis());
            virtualDao.update(virtual);
        }else {
            virtual.setVirtualStatus(AssertUtils.isNotNull(virtualRequest.getVirtualStatus())?virtualRequest.getVirtualStatus():TerminologyConfigEnum.EMAIL_STATUS.NEW.name());
            virtual.setVirtualId(DateUtils.getJobNumberByTime2("VIRTUAL",null,DateUtils.FORMATE53,false));
            virtual.setCreatedDate(System.currentTimeMillis());
            virtualDao.insert(virtual);
        }
        return resultObject;
    }

    private void vaildVirtualParameter(VirtualRequest virtualRequest) {
        AssertUtils.isNotNull(this.getLogger(),virtualRequest.getServiceName(), BaseErrorConfigEnum.SERVICE_NAME_IS_NOT_NULL);
        AssertUtils.isNotNull(this.getLogger(),virtualRequest.getServiceUser(), BaseErrorConfigEnum.SERVICE_USER_IS_NOT_NULL);
        AssertUtils.isNotNull(this.getLogger(),virtualRequest.getVirtualName(), BaseErrorConfigEnum.VIRTUAL_NAME_IS_NOT_NULL);
        AssertUtils.isNotNull(this.getLogger(),virtualRequest.getSite(), BaseErrorConfigEnum.SITE_IS_NOT_NULL);

    }
}
